/*
	Copyright (C) 2010 Tim Beyer
	
    This file is part of Pixelbots.

    Pixelbots is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Pixelbots is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Pixelbots.  If not, see <http://www.gnu.org/licenses/>.
	
*/

function attachHandlers(){
		//attach handlers
		
		//download button
		//$("#downloadButton").click(function() {
		//	window.open(displayImage.toPNG());
		//	return false;
		//});
		
		//settings toggle button
		$("#settingsButton").click(function() {
			$("#settings").toggle("slide",{direction:"down"},500);
			return false;
		});
		
		$("#imagesButton").click(function() {
			$("#images").toggle("slide",{direction:"down"},500);
			return false;
		});
		
		//when the window is resized, update UI
		$(window).resize(function() {
			if(initDone)
				updateUI();
		});
		
		$("#startButton").click(function(){
			if(!running && !paused){
				start();
			}
			else if(running || paused){
				paused = false;
				stop();
			}
			updateForms();		
		});
		
		$("#pauseButton").click(function(){
			pause();
			updateForms();					
		});
		
		$("#downloadButton").click(function(){
			//if running take the display image, else the default image
			if(running){
				window.open(displayImage.toPNG());		
			}
			else{
				window.open(originalImage.toPNG());			
			}
		});
		
		$("#invertImage").click(function(){
			originalImage.invert();
			originalImage.drawToCanvas(ctx,0,0);
		});
		
		$("#grayscaleImage").click(function(){
			originalImage.grayScale();
			originalImage.drawToCanvas(ctx,0,0);
		});
		

		function fireMoveEvents(event){
			if(typeof fireMoveEvents.barShown == "undefined"){
				fireMoveEvents.barShown = false;//initialize variable
			}
			var isInCanvas = inCanvas(event.pageX, event.pageY);
			if(isInCanvas){
				//dirty fix for UI problem with firefox
				updateUIPositions();
				//show the imageEdit options when in canvas if the simulation is not running
				if(!running){
					document.getElementById("imageEdit").style.visibility="visible";
				}
				else{
					document.getElementById("imageEdit").style.visibility="hidden";
				}
			}	
			else{
				document.getElementById("imageEdit").style.visibility="hidden";
			}
			if(this.barShown){
				//we see the bar so we don't need to call the show function
				if(!isInCanvas){
					//we see the bar and we are not in the canvas anymore
					//play the hide animation
					$("#controlsBar").hide("slide",{direction:"down"},200);			
					this.barShown = false;
				}
			}
			else{
				//the bar is not shown so we have to check whether we are in the canvas;
				if(isInCanvas){
					$("#controlsBar").show("slide",{direction:"down"},300);
					this.barShown = true;
				}
			}						
		}
	
		
		$(window).mousemove(function(event){
			fireMoveEvents(event);
		
		});

}